package cn.zhiquxing.config;

import cn.zhiquxing.enums.BusinessCode;
import cn.zhiquxing.pipeline.ProcessController;
import cn.zhiquxing.pipeline.ProcessTemplate;
import cn.zhiquxing.recall.RecallAssembleAction;
import cn.zhiquxing.recall.RecallMqAction;
import cn.zhiquxing.send.SendAfterCheckAciton;
import cn.zhiquxing.send.SendAssembleAction;
import cn.zhiquxing.send.SendMqAction;
import cn.zhiquxing.send.SendPreCheckAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * pipeline配置类
 */
@Configuration
public class PipelineConfig {
    
    @Autowired
    private SendPreCheckAction sendPreCheckAction;
    
    @Autowired
    private SendAssembleAction sendAssembleAction;
    
    @Autowired
    private SendAfterCheckAciton sendAfterCheckAciton;
    
    @Autowired
    private SendMqAction sendMqAction;
    
    @Autowired
    private RecallAssembleAction recallAssembleAction;
    
    @Autowired
    private RecallMqAction recallMqAction;

    /**
     * 1、前置参数校验
     * 2、组装发送参数
     * 3、后置参数校验
     * 4、发送消息到MQ
     * @return
     */
    @Bean("commmonSendTemplate")
    public ProcessTemplate commonSendTemplate() {
        ProcessTemplate processTemplate = new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(sendPreCheckAction, sendAssembleAction, sendAfterCheckAciton, sendMqAction));
        return processTemplate;
    }
    
    @Bean("recallMessageTemplate")
    public ProcessTemplate recallMessageTemplate() {
        ProcessTemplate processTemplate = new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(recallAssembleAction, recallMqAction));
        return processTemplate;
    }

    /**
     * pipeline流程控制器
     * @return
     */
    @Bean("apiProcessController")
    public ProcessController apiProcessController() {
        ProcessController processController = new ProcessController();
        Map<String, ProcessTemplate> processTemplateMap = new HashMap<>(4); 
        processTemplateMap.put(BusinessCode.COMMON_SEND.getCode(), commonSendTemplate());
        processTemplateMap.put(BusinessCode.RECALL.getCode(), recallMessageTemplate());
        processController.setTemplateConfig(processTemplateMap);
        return processController;
    }
    
}
